
package com.wobaby.security;

import java.util.HashMap;
import java.util.Map;

// Referenced classes of package com.wobaby.security:
//            UserPermissions, UserGroupPermissions

public class PermissionCache
{

    public PermissionCache()
    {
    }

    public static UserPermissions getUserPermissions(Integer userID)
    {
        UserPermissions u = (UserPermissions)allUserPermissions.get(userID);
        if(u == null)
        {
            u = new UserPermissions(userID);
            synchronized(allUserPermissions)
            {
                allUserPermissions.put(userID, u);
            }
        }
        return u;
    }

    public static UserGroupPermissions getUserGroupPermissions(Integer userGroupID)
    {
        UserGroupPermissions u = (UserGroupPermissions)allUserGroupPermissions.get(userGroupID);
        if(u == null)
        {
            u = new UserGroupPermissions(userGroupID);
            synchronized(allUserGroupPermissions)
            {
                allUserGroupPermissions.put(userGroupID, u);
            }
        }
        return u;
    }

    public static void reset()
    {
        reset(false);
    }

    public static void reset(boolean fromRemote)
    {
        synchronized(allUserPermissions)
        {
            allUserPermissions = new HashMap();
        }
        synchronized(allUserGroupPermissions)
        {
            allUserGroupPermissions = new HashMap();
        }
        if(!fromRemote)
            resetRemote();
    }

    public static void resetUserGroupPermissions(Integer userGroupID)
    {
        resetUserGroupPermissions(userGroupID, false);
    }

    public static void resetUserGroupPermissions(Integer userGroupID, boolean fromRemote)
    {
        synchronized(allUserGroupPermissions)
        {
            if(allUserGroupPermissions != null)
                allUserGroupPermissions.remove(userGroupID);
        }
        if(!fromRemote)
            resetRemote();
    }

    public static void resetUserPermissions(Integer userID)
    {
        resetUserPermissions(userID, false);
    }

    public static void resetUserPermissions(Integer userID, boolean fromRemote)
    {
        synchronized(allUserPermissions)
        {
            if(allUserPermissions != null)
                allUserPermissions.remove(userID);
        }
        if(!fromRemote)
            resetRemote();
    }

    private static void resetRemote()
    {
        //RemoteUpdateThread.broadcast(new ReloadPermissionUpdater());
    }

    private static Map allUserPermissions = new HashMap(20);
    private static Map allUserGroupPermissions = new HashMap(20);

}